#region File Description
//-----------------------------------------------------------------------------
// MenuEntry.cs
//
// XNA Community Game Platform
// Copyright (C) Microsoft Corporation. All rights reserved.
//-----------------------------------------------------------------------------
// Modified by Hristo Hristov (15.09.11):
//
// Revision 4
#endregion

#region Using Statements
using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using NOVA.ScreenManagement;
using NOVA.ScreenManagement.BaseScreens;
#endregion

namespace NOVA.ScreenManagement
{
    /// <summary>
    /// Helper class represents a single entry in a MenuScreen. By default this
    /// just draws the entry text string, but it can be customized to display menu
    /// entries in different ways. This also provides an event that will be raised
    /// when the menu entry is selected.
    /// </summary>
    public class MenuEntry
    {
        #region Fields

        /// <summary>
        /// The text rendered for this entry.
        /// </summary>
        string text;

        /// <summary>
        /// Tracks a fading selection effect on the entry.
        /// </summary>
        /// <remarks>
        /// The entries transition out of the selection effect when they are deselected.
        /// </remarks>
        float selectionFade;

        /// <summary>
        /// The position at which the entry is drawn. 
        /// </summary>
        Vector2 position;

        Vector2 customPosition;

        Color color = Color.White;

        Color highlightColor = Color.Yellow;

        float rotation = 0f;

        float m_scale;

        SpriteFont m_font;

        bool useCustomPosition = false;

        #endregion

        #region Properties

        /// <summary>
        /// Gets or sets the font of the menu entry.
        /// </summary>
        public SpriteFont Font
        {
            get { return m_font; }
            set { m_font = value; }
        }

        /// <summary>
        /// Gets or sets the scale of this menu entry.
        /// </summary>
        public float Scale
        {
            get { return m_scale; }
            set { m_scale = value; }
        }

        /// <summary>
        /// Gets or sets the text of this menu entry.
        /// </summary>
        public string Text
        {
            get { return text; }
            set { text = value; }
        }


        /// <summary>
        /// Gets or sets the position at which to draw this menu entry.
        /// </summary>
        /// <remarks>This is position is set automatically.</remarks>
        public Vector2 Position
        {
            get { return this.position; }
            internal set { this.position = value; }
        }

        /// <summary>
        /// Gets or sets the custom position at which to draw this menu entry.
        /// </summary>
        public Vector2 CustomPosition
        {
            get { return this.customPosition; }
            set { this.customPosition = value; }
        }

        /// <summary>
        /// Gets or sets the color of the menu entry.
        /// </summary>
        public Color Color
        {
            get { return color; }
            set { color = value; }
        }

        /// <summary>
        /// Gets or sets the highlight color of the menu entry, when the entry is selected.
        /// </summary>
        public Color HighlightColor
        {
            get { return highlightColor; }
            set { highlightColor = value; }
        }

        /// <summary>
        /// Gets or sets the rotation of the menu entry in radians.
        /// </summary>
        public float Rotation
        {
            get { return rotation; }
            set { rotation = value; }
        }

        /// <summary>
        /// Gets or sets a flag whether a custom position should be used
        /// or if the menu entry should be positioned automatically.
        /// </summary>
        public bool UseCustomPosition
        {
            get { return this.useCustomPosition; }
            set { this.useCustomPosition = value; }
        }

        #endregion

        #region Events


        /// <summary>
        /// Event raised when the menu entry is selected.
        /// </summary>
        public event EventHandler<PlayerIndexEventArgs> Selected;


        /// <summary>
        /// Method for raising the Selected event.
        /// </summary>
        protected internal virtual void OnSelectEntry(PlayerIndex playerIndex)
        {
            if (Selected != null)
                Selected(this, new PlayerIndexEventArgs(playerIndex));
        }


        #endregion

        #region Initialization


        /// <summary>
        /// Constructs a new menu entry with the specified text.
        /// </summary>
        public MenuEntry(string text)
        {
            this.text = text;
            m_scale = 1f;
        }


        #endregion

        #region Update and Draw


        /// <summary>
        /// Updates the menu entry.
        /// </summary>
        public virtual void Update(MenuScreen screen, bool isSelected, GameTime gameTime)
        {
            // there is no such thing as a selected item on Windows Phone, so we always
            // force isSelected to be false
#if WINDOWS_PHONE
            isSelected = false;
#endif

            // When the menu selection changes, entries gradually fade between
            // their selected and deselected appearance, rather than instantly
            // popping to the new state.
            float fadeSpeed = (float)gameTime.ElapsedGameTime.TotalSeconds * 4;

            if (isSelected)
                selectionFade = Math.Min(selectionFade + fadeSpeed, 1);
            else
                selectionFade = Math.Max(selectionFade - fadeSpeed, 0);
        }


        /// <summary>
        /// Draws the menu entry. This can be overridden to customize the appearance.
        /// </summary>
        public virtual void Draw(MenuScreen screen, bool isSelected, GameTime gameTime)
        {
            // there is no such thing as a selected item on Windows Phone, so we always
            // force isSelected to be false
#if WINDOWS_PHONE
            isSelected = false;
#endif

            // Draw the selected entry in yellow, otherwise white.
            Color clr = isSelected ? highlightColor : color;

            // Pulsate the size of the selected menu entry.
            double time = gameTime.TotalGameTime.TotalSeconds;
            
            float pulsate = (float)Math.Sin(time * 6) + 1;

            float scale = m_scale + pulsate * 0.05f * selectionFade;

            // Modify the alpha to fade text out during transitions.
            clr *= screen.TransitionAlpha;

            // Draw text, centered on the middle of each line.
            ScreenManager screenManager = Core.ScreenManager;
            SpriteBatch spriteBatch = screenManager.SpriteBatch;
            
            SpriteFont font = (m_font == null) ? screenManager.Font : m_font;

            Vector2 origin = new Vector2(0, font.LineSpacing / 2);

            Vector2 pos = Vector2.Zero;

            if (this.useCustomPosition)
            {
                pos = this.customPosition;
            }
            else
            {
                pos = this.position;
            }

            spriteBatch.DrawString(font, text, pos, clr, rotation,
                                   origin, scale, SpriteEffects.None, 0);            
        }


        /// <summary>
        /// Queries how much space this menu entry requires.
        /// </summary>
        public virtual int GetHeight(MenuScreen screen)
        {
            return /*screen*/Core.ScreenManager.Font.LineSpacing;
        }


        /// <summary>
        /// Queries how wide the entry is, used for centering on the screen.
        /// </summary>
        public virtual int GetWidth(MenuScreen screen)
        {
            return (int)/*screen*/Core.ScreenManager.Font.MeasureString(Text).X;
        }


        #endregion
    }
}
